#pragma once

#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;

/*HJJ QQ479287006
 *
 * */
vector<int> spiralOrder(vector<vector<int>> &matrix) {

    int left = 0;
    int right = matrix[0].size() - 1;
    int high = 0;
    int low = matrix.size() - 1;
    vector<int> v;

    while (high <= low && left <= right) {

        int temp = left;

        if (temp > right)
            break;

        while (temp <= right) {

            v.push_back(matrix[high][temp]);
            temp++;
        }
        high++;
        temp = high;
        if (temp > low)
            break;
        while (temp <= low) {

            v.push_back(matrix[temp][right]);
            temp++;
        }
        right--;
        temp = right;
        if (temp < left)
            break;
        while (temp >= left) {

            v.push_back(matrix[low][temp]);
            temp--;
        }

        low--;
        temp = low;

        if (temp < high)
            break;
        while (temp >= high) {
            v.push_back(matrix[temp][left]);
            temp--;
        }
        left++;


    }


    return v;

}
